﻿using System.Collections.Generic;
using System.Linq;

namespace timestamp
{
	public class Options
	{
		#region Constructor

		public Options()
		{
			this.Format = "yyMMdd_HHmm";
			this.Test = string.Empty;
		}

		#endregion

		#region Properties

		public string Format { get; set; }

		public string Test { get; set; }

		#endregion

		#region Private Methods

		private void SetProperty(string propertyName, string value)
		{
			typeof(Options).GetProperty(propertyName).SetValue(this, value, null);
		}

		#endregion

		#region !! CONFIGURATION

		private static void OptionListConfiguration(Dictionary<string, string> optionsList)
		{
			optionsList.Add("-f", "Format");
			//optionsList.Add("-test", "Test");
		}

		#endregion

		#region Static

		private static Dictionary<string, string> optionsList;
		private static Dictionary<string, string> OptionsList
		{
			get
			{
				if (optionsList == null)
				{
					optionsList = new Dictionary<string, string>();
					OptionListConfiguration(optionsList);
				}
				return optionsList;
			}
		}

		public static Options CreateOptions(string[] args)
		{
			Options options = new Options();
			foreach (string item in args.Where(i => i.IndexOf("-") == 0))
			{
				var query = Options.OptionsList.Where(keyValue => keyValue.Key == item);
				if (query.Count() == 1)
				{
					KeyValuePair<string, string> keyValue = query.Single();
					string value = GetParamValue(args, item);
					if (!string.IsNullOrEmpty(value))
					{
						options.SetProperty(keyValue.Value, value);
					}
				}
			}
			return options;
		}

		private static string GetParamValue(string[] args, string parameter)
		{
			int count = 0;
			foreach (string param in args)
			{
				if (param == parameter)
				{
					if (args.Length - 1 >= count + 1)
					{
						return args[count + 1];
					}
					else
					{
						break;
					}
				}
				count++;
			}
			return string.Empty;
		}

		#endregion
	}
}
